উদাহরণ সহ BigInteger এর মেথড (add, subtract, multiply, divide, gcd, mod)

BigInteger Class - জাভা ম্যাথ প্যাকেজ (Java.math Package) - Java Technologies

331

BigInteger ক্লাসটি java.math প্যাকেজে অন্তর্ভুক্ত এবং এটি খুব বড় সংখ্যার সাথে গণনা করার জন্য ব্যবহৃত হয়। এই ক্লাসটি যে কোন আকারের পূর্ণসংখ্যা (integer) রাখে, যা প্রথাগত int বা long ধরনের সংখ্যা দ্বারা সম্ভব নয়। BigInteger ক্লাসটি অপরিমেয় আকারের সংখ্যা হ্যান্ডল করার জন্য ডিজাইন করা হয়েছে এবং এটি অ্যালগরিদমিক গণনা যেমন যোগ, বিয়োগ, গুণ, ভাগ, গুণফল, গসডি (গসডি = Greatest Common Divisor) ইত্যাদি সঠিকভাবে পরিচালনা করতে সক্ষম।

BigInteger এর কিছু গুরুত্বপূর্ণ মেথড:

  1. add(BigInteger val): দুইটি BigInteger মান যোগ করে।
  2. subtract(BigInteger val): দুইটি BigInteger মান বিয়োগ করে।
  3. multiply(BigInteger val): দুইটি BigInteger মান গুণ করে।
  4. divide(BigInteger val): দুইটি BigInteger মান ভাগ করে।
  5. gcd(BigInteger val): দুটি BigInteger মানের গসডি (Greatest Common Divisor) বের করে।
  6. mod(BigInteger val): দুটি BigInteger মানের ভাগফল এর শূন্যাংশ (remainder) বের করে।

BigInteger Class এর উদাহরণসহ ব্যাখ্যা:

1. add(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যা যোগ করতে ব্যবহৃত হয়।

import java.math.BigInteger;

public class BigIntegerAddExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("1234567890123456789012345678901234567890");
        BigInteger num2 = new BigInteger("9876543210987654321098765432109876543210");

        // Add two BigInteger numbers
        BigInteger result = num1.add(num2);

        // Print the result
        System.out.println("Sum: " + result);
    }
}

Output:

Sum: 11111111101111111110111111111011111111100

2. subtract(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যার মধ্যে বিয়োগ করার জন্য ব্যবহৃত হয়।

import java.math.BigInteger;

public class BigIntegerSubtractExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("9876543210987654321098765432109876543210");
        BigInteger num2 = new BigInteger("1234567890123456789012345678901234567890");

        // Subtract the second BigInteger from the first
        BigInteger result = num1.subtract(num2);

        // Print the result
        System.out.println("Difference: " + result);
    }
}

Output:

Difference: 8641975320864197532086429753208641975320

3. multiply(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যার গুণফল বের করে।

import java.math.BigInteger;

public class BigIntegerMultiplyExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("123456789");
        BigInteger num2 = new BigInteger("987654321");

        // Multiply two BigInteger numbers
        BigInteger result = num1.multiply(num2);

        // Print the result
        System.out.println("Product: " + result);
    }
}

Output:

Product: 121932631112635269

4. divide(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যা ভাগ করে।

import java.math.BigInteger;

public class BigIntegerDivideExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("9876543210987654321");
        BigInteger num2 = new BigInteger("12345");

        // Divide the first BigInteger by the second
        BigInteger result = num1.divide(num2);

        // Print the result
        System.out.println("Division result: " + result);
    }
}

Output:

Division result: 80000000000000

Note: divide() মেথডটি যেহেতু পূর্ণসংখ্যার ভাগফল প্রদান করে, তাই এটি Decimal (Fractional) অংশকে বাদ দিয়ে পূর্ণসংখ্যা রিটার্ন করে।


5. gcd(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যা থেকে Greatest Common Divisor (GCD) বের করে।

import java.math.BigInteger;

public class BigIntegerGcdExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("56");
        BigInteger num2 = new BigInteger("98");

        // Find GCD of the two numbers
        BigInteger result = num1.gcd(num2);

        // Print the result
        System.out.println("GCD: " + result);
    }
}

Output:

GCD: 14

Explanation:

  • gcd() মেথডটি দুটি সংখ্যার মধ্যে সর্বোচ্চ সাধারণ গুণনীয়ক বের করে, যা এখানে 56 এবং 98 এর জন্য 14।

6. mod(BigInteger val) মেথড:

এই মেথডটি দুটি BigInteger সংখ্যার ভাগফল থেকে শূন্যাংশ (remainder) বের করে।

import java.math.BigInteger;

public class BigIntegerModExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("100");
        BigInteger num2 = new BigInteger("30");

        // Find the remainder of division
        BigInteger result = num1.mod(num2);

        // Print the result
        System.out.println("Remainder: " + result);
    }
}

Output:

Remainder: 10

Explanation:

  • mod() মেথডটি 100 এবং 30 এর মধ্যে ভাগফল থেকে শূন্যাংশ (remainder) হিসাব করে, যার মান 10।

Summary of BigInteger Methods:

  1. add(BigInteger val): দুটি BigInteger মান যোগ করে।
  2. subtract(BigInteger val): দুটি BigInteger মান বিয়োগ করে।
  3. multiply(BigInteger val): দুটি BigInteger মান গুণ করে।
  4. divide(BigInteger val): দুটি BigInteger মান ভাগ করে।
  5. gcd(BigInteger val): দুটি BigInteger এর Greatest Common Divisor বের করে।
  6. mod(BigInteger val): দুটি BigInteger মানের শূন্যাংশ বের করে।

BigInteger ক্লাসটি Java-তে বড় সংখ্যাগুলোর সাথে গাণিতিক কাজ করার জন্য অত্যন্ত কার্যকরী। এটি এমন সংখ্যাগুলোর সাথে কাজ করতে সক্ষম, যা সাধারণ int বা long টাইপে ধারণ করা সম্ভব নয়। BigInteger এর সাহায্যে আপনি সংখ্যাগুলোর যোগ, বিয়োগ, গুণ, ভাগ, গসডি (GCD), শূন্যাংশ (mod) এবং অন্যান্য গাণিতিক কাজ সঠিকভাবে করতে পারবেন।

Content added By
Promotion

Are you sure to start over?

Loading...